package com.example.demo.service;

import com.example.demo.entity.Music;
import com.example.demo.entity.MusicCollection;
import com.example.demo.entity.MusicQuery;
import com.example.demo.mapper.MusicCollectionMapper;

import com.example.demo.vo.ViewMusicCollectionInfo;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MusicCollectionImpl implements MusicCollectionService{
    @Autowired
    private MusicCollectionMapper musicCollectionMapper;

    //根据用户ID查找对应用户喜欢的笔记
    @Override
    public PageInfo<ViewMusicCollectionInfo> findMusicCollectionInfo(MusicQuery query, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ViewMusicCollectionInfo> list = musicCollectionMapper.findMusicCollectionInfoId(query);
        PageInfo<ViewMusicCollectionInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    @Override
    public Map<String, Integer> countMusicTypeByUserId(Integer user_id) {
        List<Map<String, Object>> typeCounts = musicCollectionMapper.countMusicTypeByUserId(user_id);
        Map<String, Integer> typeCountMap = new HashMap<>();
        for (Map<String, Object> typeCount : typeCounts) {
            // 显式转换Long为Integer
            String type = (String) typeCount.get("type");
            Integer count = ((Long) typeCount.get("count")).intValue();
            typeCountMap.put(type, count);
        }
        return typeCountMap;
    }



    //根据用户id和笔记id，收藏笔记。
    @Override
    public boolean insertMusicCollection(MusicCollection musicCollection) {
        return musicCollectionMapper.insertMusicCollection(musicCollection) > 0;

    }

    //取消收藏，笔记详情页
    @Override
    public boolean deleteMusicCollection1(MusicCollection musicCollection) {
        return musicCollectionMapper.deleteMusicCollection1(musicCollection.getUser_id(), musicCollection.getmusic_id()) > 0;
    }

    //查询是否已经收藏过
    @Override
    public boolean checkIfMusicCollection(Integer user_id, Integer music_id) {
        return musicCollectionMapper.checkIfMusicCollection(user_id, music_id) > 0;
    }


}
